package com.knowswift.project.tasks;

import com.knowswift.common.bean.issue.po.Issue;
import com.knowswift.common.bean.issue.po.MonitorDevice;
import com.knowswift.common.bean.issue.po.MonitorRecord;
import com.knowswift.common.bean.project.po.ModulePermission;
import com.knowswift.common.bean.project.vo.ProjectLimit;
import com.knowswift.common.bean.project.vo.ProjectVO;
import com.knowswift.common.utils.WrappedBeanCopier;
import com.knowswift.project.service.IssueService;
import com.knowswift.project.service.ModulePermissionService;
import com.knowswift.project.service.MonitorDeviceService;
import com.knowswift.project.service.MonitorRecordService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Component
@EnableAsync
public class AsyncTask {

    @Resource
    private IssueService issueService;

    @Resource
    private ModulePermissionService modulePermissionService;

    @Resource
    private MonitorDeviceService monitorDeviceService;

    @Resource
    private MonitorRecordService monitorRecordService;

    @Async("taskExecutor")
    public void updateData(String moduleId, String oldModuleName, String newModuleName, List<ProjectVO> projectVOS) {
        System.out.println("=====================>>>开始更新数据");
        System.out.println("=====================>>>moduleId:" + moduleId);
        System.out.println("=====================>>>oldModuleName:" + oldModuleName);
        System.out.println("=====================>>>newModuleName:" + newModuleName);
        System.out.println("=====================>>>更新其他表的数据:" + projectVOS);
        List<Issue> issues = issueService.lambdaQuery().eq(Issue::getModuleName, oldModuleName).list();
        for (Issue issue : issues) {
            issue.setModuleName(newModuleName);
            projectVOS.stream().filter(projectVO -> projectVO.getOldName().equals(issue.getProjectName()))
                    .findFirst().ifPresent(projectVO -> issue.setProjectName(projectVO.getProjectName()));
        }
        if (!CollectionUtils.isEmpty(issues)){
            issueService.updateBatchById(issues);
        }

//        List<ModulePermission> permissions = modulePermissionService.lambdaQuery().eq(ModulePermission::getModuleId, moduleId).list();
        List<ModulePermission> permissions = modulePermissionService.getBaseMapper().getByModuleId(moduleId);
        System.out.println("=====================>>>permissions:" + permissions);
        for (ModulePermission permission : permissions) {
            List<ProjectLimit> projectLimits = WrappedBeanCopier.JSONArrayToList(permission.getProjects(), ProjectLimit.class);
            System.out.println("=====================>>>projectLimits:" + projectLimits);
            List<ProjectLimit> list = new ArrayList<>();
            for (ProjectVO projectVO : projectVOS) {
                for (ProjectLimit projectLimit : projectLimits) {
                    if (projectLimit.getProjectName().equals(projectVO.getProjectName())
                            || projectLimit.getProjectName().equals(projectVO.getOldName())) {
                        ProjectLimit limit = new ProjectLimit();
                        limit.setProjectOrder(projectVO.getProjectOrder());
                        limit.setProjectName(projectVO.getProjectName());
                        limit.setLimit(projectLimit.getLimit());
                        list.add(limit);
                    }
                }
            }
            list = list.stream().sorted(Comparator.comparing(ProjectLimit::getProjectOrder)).collect(Collectors.toList());
            permission.setProjects(list);
        }
        System.out.println("=====================>>>new permissions:" + permissions);
        if (!CollectionUtils.isEmpty(permissions)){
            modulePermissionService.updateBatchById(permissions);
        }


    }

    @Async("taskExecutor")
    public void updateMonitorData(String oldModuleName, String newModuleName, List<ProjectVO> projectVOS) {
        // TODO 更新设备表的模块名 项目名
        List<MonitorDevice> monitorDevices = monitorDeviceService.lambdaQuery().eq(MonitorDevice::getModuleName, oldModuleName).list();
        for (MonitorDevice monitorDevice : monitorDevices) {
            monitorDevice.setModuleName(newModuleName);
            projectVOS.stream().filter(projectVO -> projectVO.getOldName().equals(monitorDevice.getProjectName()))
                    .findFirst().ifPresent(projectVO -> monitorDevice.setProjectName(projectVO.getProjectName()));
        }
        if (!CollectionUtils.isEmpty(monitorDevices)){
            monitorDeviceService.updateBatchById(monitorDevices);
        }

        // TODO 更新记录表的模块名 项目名
        List<MonitorRecord> monitorRecords = monitorRecordService.lambdaQuery().eq(MonitorRecord::getModuleName, oldModuleName).list();
        for (MonitorRecord monitorRecord : monitorRecords) {
            monitorRecord.setModuleName(newModuleName);
            projectVOS.stream().filter(projectVO -> projectVO.getOldName().equals(monitorRecord.getProjectName()))
                    .findFirst().ifPresent(projectVO -> monitorRecord.setProjectName(projectVO.getProjectName()));
        }
        if (!CollectionUtils.isEmpty(monitorRecords)){
            monitorRecordService.updateBatchById(monitorRecords);
        }
    }
}
